<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>${empty student ? '添加' : '编辑'}学生</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.6.8/css/layui.css">
    <script src="https://www.layuicdn.com/layui-v2.6.8/layui.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        .form-container {
            padding: 20px;
        }
    </style>
</head>
<body>
    <div class="form-container">
        <form class="layui-form" method="post" action="${pageContext.request.contextPath}/admin/student/save">
            <c:if test="${not empty student}">
                <input type="hidden" name="sno" value="${student.sno}">
            </c:if>
            
            <div class="layui-form-item">
                <label class="layui-form-label">学号</label>
                <div class="layui-input-block">
                    <input type="text" name="sno" required lay-verify="required|number" 
                           placeholder="请输入学号" class="layui-input" value="${student.sno}"
                           ${not empty student ? 'readonly' : ''}>
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="sname" required lay-verify="required" 
                           placeholder="请输入姓名" class="layui-input" value="${student.sname}">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="ssex" value="1" title="男" ${empty student || student.ssex == 1 ? 'checked' : ''}>
                    <input type="radio" name="ssex" value="0" title="女" ${student.ssex == 0 ? 'checked' : ''}>
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">年龄</label>
                <div class="layui-input-block">
                    <input type="number" name="sage" required lay-verify="required|number" 
                           placeholder="请输入年龄" class="layui-input" value="${student.sage}">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">院系</label>
                <div class="layui-input-block">
                    <input type="text" name="sdept" required lay-verify="required" 
                           placeholder="请输入院系" class="layui-input" value="${student.sdept}">
                </div>
            </div>
            
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="studentForm">保存</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>

    <script>
        layui.use(['form', 'layer'], function(){
            var form = layui.form;
            var layer = layui.layer;
            
            form.on('submit(studentForm)', function(data){
                var index = parent.layer.getFrameIndex(window.name);
                
                $.ajax({
                    url: data.form.action,
                    type: 'POST',
                    data: data.field,
                    success: function(res){
                        if(res.code === 0){
                            parent.layer.msg('保存成功', {icon: 1});
                            parent.layer.close(index);
                            parent.location.reload();
                        } else {
                            layer.msg(res.msg || '保存失败', {icon: 2});
                        }
                    },
                    error: function(){
                        layer.msg('系统错误', {icon: 2});
                    }
                });
                return false;
            });
        });
    </script>
</body>
</html> 